<?php
class MY_Controller extends Controller
{
	private $resource = null; 
	private $priviledge = null;
	private $default_role = 'Guest';
	private $ci;
	private $acl;
	private $language;
	
	public
		$layout = 'default',
		$view = 'index',
		$data = array(),
		$user = array()
	;
	
	function __construct()
	{
		parent::__construct();

		//defines layout
		$this->layout = 'page';
	
		$this->data['user_role'] = strtolower($this->auth->getUserData('role'));
		
		//defines view
		$this->view = $this->router->fetch_class() . '/' . $this->router->fetch_method();

		$this->load->model(array('/site/page_model', '/public/homepage_model', '/public/inbox_model'));
		
        //$this->data['news_block'] = $this->page_model->getNewsBlock(PAGE_CATEGORY_USER);
		
        switch ($this->auth->getUserGroup())
        {
        	case 'Contributor':
        		$this->meta['title'] = T_('HN Books - Contributors');
        		break;
        		
        	case 'Company':
        		$this->meta['title'] = T_('HN Books - Companies');
        		$this->data['tests_portfolio'] = $this->db->query('SELECT COUNT(id_tests) AS num FROM tests_portfolio WHERE id_companies='.$this->db->escape($this->auth->getID()))->row();
        		break;
        		
        	default :
        		$this->meta['title'] = T_('HN Books Connections');
        		break;
        }

		$this->meta['description'] = T_('Website description');
		$this->meta['keywords'] = T_('Website keywords');
		//check single login

		$id_user = $this->auth->getID();
		$is_logged = $this->auth->is_logged_in();
		if ($is_logged)
		{
            $this->data['is_logged'] = $is_logged;
            $this->inbox_model->userID = $id_user;
            $this->data['inbox_number'] = $this->inbox_model->countNewInbox();
		}	

		$this->acl = init_roles();	
		  
		$router =& load_class('Router');
		$this->resource   = $router->fetch_class(); 
		$this->priviledge = $router->fetch_method();   
		$this->ci =& get_instance();		
		$acl = $this->getACL();	

		
        $session_data = $this->auth->getUserData();
        // Defines the language
        $lang = $this->uri->segment(1);
        // Loads nedeed language
      //  pr($session_data);
        if (strlen($lang) == '2')
        {           
            $session_data['language'] = $lang;
            //$this->auth->setUserData($session_data);
            $this->language = $lang;
            $this->lang->load('str', $lang);
        }
        else 
        {
            // By default
            if (empty($session_data['language']))
            {            	
            	$this->language = 'en';
                $this->lang->load('str', 'en');
            }
            else
            {
                $this->language = $session_data['language'];
                $this->lang->load('str', $session_data['language']);                
            }   
        }
          
        $session_data['language'] = $this->language; 
        $this->auth->setUserData($session_data);
        
        $this->language = ($this->input->get_post('lang')) ? $this->input->get_post('lang') : $this->language;
		
        
		if(!empty($id_user))
		{
			$this->setDefaultRole($this->auth->getUserGroup());
		}
		
		if(!$acl->isAllowed($this->getDefaultRole(), $this->getResource(), $this->getPriviledge()) && $this->getDefaultRole() == 'Guest')
		{
			redirect('user/login', 'refresh');
		}		
   /*
		// Defines layout
		$this->layout = 'page';
		
		// Defines view
		$this->view = $this->router->fetch_class() . '/' . $this->router->fetch_method();

		// Checks single login
		if (!$this->auth->is_logged_in() && $this->view !== 'advertisers/login' && $this->router->method !== 'password_forgot')
		{
			redirect('advertisers/login');
		}	
		else
		{
			$this->db->where('id', $this->auth->getID());
			$this->user = $this->db->get('advertisers')->row();
		}*/

		//ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.BASEPATH.'extensions/PEAR/');		
	}
	
	function renderLayout($print = FALSE)
	{		
		if ($print == FALSE)
		{
			$this->load->view('_layouts/' . $this->layout, $this->data, $print);	
		}
		else
		{			
			return $this->load->view('_layouts/' . $this->layout, $this->data, $print);
		}		
	}

	//TODO: does not work in layout view - CI_loader
	function renderElement($element = null)
	{
		$this->load->view('_elements/' . $element, $this->data);
	}
	
   public function getInstance(){
      return $this->ci;
   }

   public function getACL(){
      return $this->acl;
   }

   public function getDefaultRole(){
      return $this->default_role;
   }

   public function getPriviledge(){
      return $this->priviledge;
   }
   public function getResource(){
      return $this->resource;
   }

   public function setDefaultRole($role){
      $this->default_role = $role;
   }

   public function getLang(){
      return $this->language;
   }	
	
}
?>